<template>
    <div>
        <div v-if="userGroup === '求职者' && !alreadySendResume" class="sendDiv">
            <el-button type="success" @click="toSendResume" style="margin-bottom:10px">投递简历</el-button><br>
            <span style="color:red;margin-bottom:10px" v-if="isSendResume && !isResumeListNull">请选择要投递的简历</span><br>
            <el-select v-model="currentResumeId" placeholder="" style="margin-top:10px;margin-bottom:10px"
                v-if="!isResumeListNull">
                <el-option v-for="resume in resumeList" :key="resume.resume_management_id"
                    :value="resume.resume_management_id" :label="resume.resume_name"></el-option>
            </el-select>
            <el-button type="primary" @click="doSendResume" style="margin-left:10px"
                v-if="isSendResume && !isResumeListNull">提交简历</el-button>
            <div v-if="isResumeListNull && isSendResume">
                <span style="color:red;margin-bottom:10px">您暂时简历还没有简历!</span><br>
                <el-button type="primary" @click="toCreateResume" style="margin-top:10px">去创建简历</el-button>
            </div>
        </div>
        <h1 v-if="alreadySendResume && userGroup === '求职者'" style="color:green">已投递简历!</h1>
        <div>
            <el-card :body-style="{ padding: '20px' }">
                <h2>
                    <el-card size="medium">职位名称:{{ jobRecruitment.job_title }}</el-card>
                </h2>
                <h3>
                    <el-card size="medium" class="el-icon-menu" style="float: right;margin-buttom:50px">职位分类:{{
                        jobRecruitment.job_classification }}</el-card>
                </h3>
                <h3>
                    <el-card size="medium" class="el-icon-user-solid" style="float: left;">企业名称:{{
                        jobRecruitment.enterprise_name
                    }} &emsp;企业编号:{{ jobRecruitment.enterprise_number }}</el-card>
                </h3>
                <el-card size="medium" style="float: center;height:80px">
                    <h1 style="margin-top:-10px;margin-left:-120px;color:rgb(211, 138, 49)">薪资:{{ jobRecruitment.salary }}
                    </h1>
                </el-card>
                <div style="margin-top:50px">
                    <el-tag size="medium" class="el-icon-view">{{ jobRecruitment.hits }}</el-tag>
                    <el-tag size="medium" class="el-icon-date">发布时间:{{ format(jobRecruitment.create_time).format
                    }}</el-tag><br>
                </div>
                <div style="margin-top:50px;">
                    <h3>职位要求:</h3>
                    <p>{{ jobRecruitment.job_requirements }}</p>
                </div>
                <div style="margin-top:50px;">
                    <h3>职位待遇:</h3>
                    <p>{{ jobRecruitment.position_treatment }}</p>
                </div>
                <div style="margin-top:50px;">
                    <h3>公司简介:</h3>
                    <p>{{ jobRecruitment.company_profile }} </p>
                </div>
            </el-card>
            <div>
                <span style="color:green;margin-bottom:10px">发表评论:</span><br>
                <el-input type="textarea" v-model="commentContent" style="margin-top:10px"></el-input>
                <el-button type="success" @click="sendComment" style="margin-top:10px">评论</el-button>
            </div>
        </div>
        <router-view name="page_foot"></router-view>
    </div>
</template>
<script>
import axios from 'axios';
import dateTime from '@/utils/date_time';
export default {
    data() {
        return {
            commentContent: '',
            alreadySendResume: false,
            isResumeListNull: true,
            resumeList: [],
            currentResumeId: '',
            isSendResume: false,
            jobRecruitment: {},
            userGroup: '',
            currentResume: {},
            seeker: {},
            nickname: '',
            avatarUrl: '',
            enterpriseUserId: ''
        }
    },
    watch: {
        currentResumeId: function () {
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/resume_management/get_obj',
                params: {
                    resume_management_id: that.currentResumeId,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    if (response.data.result.obj) {
                        that.currentResume = response.data.result.obj;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        }
    },
    created: function () {
        let that = this;
        //防止刷新时跳转到其它位置
        let id = that.$route.params.job_recruitment_id;
        if (!that.$route.params.job_recruitment_id) {
            id = window.localStorage.getItem("job_recruitment_id");
        }
        axios.get('http://127.0.0.1:5000/api/job_recruitment/get_obj', {
            params: {
                job_recruitment_id: id,
            }
        })
            .then(function (response) {
                console.log(response.data.result.obj);
                that.jobRecruitment = response.data.result.obj;
                //保存到本地仓库
                window.localStorage.setItem("job_recruitment_id", response.data.result.obj.job_recruitment_id);
                if (window.localStorage.getItem("token")) {
                    //如果已登录，增加点击量
                    that.addHits(response.data.result.obj.job_recruitment_id, response.data.result.obj.hits);
                    that.userGroup = window.localStorage.getItem("user_group");
                    that.getEnterpriseUserId();
                    if (window.localStorage.getItem("user_group") === '求职者') {
                        //检查是否投递简历
                        that.checkAlreadySendResume();
                    }
                }
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    },
    methods: {
        getEnterpriseUserId() {
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/get_obj',
                params: {
                    enterprise_number: that.jobRecruitment.enterprise_number,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    if (response.data.result.obj) {
                        that.enterpriseUserId = response.data.result.obj.user_id;
                        that.getNickNameAndAvatar();
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        getNickNameAndAvatar() {
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/user/get_obj',
                params: {
                    user_id: that.enterpriseUserId,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    if (response.data.result.obj) {
                        that.nickname = response.data.result.obj.nickname;
                        let url = response.data.result.obj.avatar.split('/');
                        that.avatarUrl = 'http://127.0.0.1:5000/' + url[1] + "/" + url[2] + "/avatar/" + url[3];
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        sendComment() {
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/comment/add',
                data: {
                    user_id: window.localStorage.getItem("user_id"),
                    reply_to_id: that.enterpriseUserId,
                    content: that.commentContent,
                    nickname: that.nickname,
                    avatar: that.avatarUrl,
                    source_table: 'job_recruitment',
                    source_field: 'job_recruitment_id',
                    source_id: that.jobRecruitment.job_recruitment_id,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    if (response.data.result) {
                        that.$message.success("评论成功");
                        window.location.reload();
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        checkAlreadySendResume() {
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/job_seekers/get_obj',
                params: {
                    user_id: window.localStorage.getItem("user_id"),
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    if (!response.data.result.obj) {
                        return;
                    }
                    let id = response.data.result.obj.job_seekers_id;
                    let thats = that;
                    axios({
                        method: 'get',
                        url: 'http://127.0.0.1:5000/api/deliver_resume/get_obj',
                        params: {
                            job_recruitment_id: thats.jobRecruitment.job_recruitment_id,
                            job_seekers: id
                        },
                        headers: {
                            'X-Auth-Token': window.localStorage.getItem("token")
                        },
                    })
                        .then(function (response) {
                            if (response.data.result.obj) {
                                thats.alreadySendResume = true;
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        })
                        .then(function () {
                            // 总是会执行
                        })
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        doSendResume() {
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/deliver_resume/add',
                data: {
                    job_title: that.jobRecruitment.job_title,
                    enterprise_name: that.jobRecruitment.enterprise_name,
                    enterprise_number: that.jobRecruitment.enterprise_number,
                    job_seekers: that.seeker.job_seekers_id,
                    full_name: that.seeker.full_name,
                    resume_attachment: that.currentResume.resume_attachment,
                    resume_management_id: that.currentResume.resume_management_id,
                    resume_name: that.currentResume.resume_name,
                    job_recruitment_id: that.jobRecruitment.job_recruitment_id,
                    delivery_status: '已投递',
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    if (response.data.result) {
                        that.$message.success("投递简历成功");
                        window.location.reload();
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        toSendResume() {
            this.isSendResume = true;
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/resume_management/get_list',
                params: {
                    user: window.localStorage.getItem("user_id"),
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    if (!response.data.result.list) {
                        return;
                    }
                    that.isResumeListNull = false;
                    that.resumeList = response.data.result.list;
                    let thats = that;
                    axios({
                        method: 'get',
                        url: 'http://127.0.0.1:5000/api/job_seekers/get_obj',
                        params: {
                            user_id: window.localStorage.getItem("user_id"),
                        },
                        headers: {
                            'X-Auth-Token': window.localStorage.getItem("token")
                        },
                    })
                        .then(function (response) {
                            thats.seeker = response.data.result.obj;
                        })
                        .catch(function (error) {
                            console.log(error);
                        })
                        .then(function () {
                            // 总是会执行
                        })
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        toCreateResume() {
            this.$router.push({ name: 'resume_add' }, () => { }, () => { });
        },
        format(create_time) {
            return dateTime.dateTime(create_time)
        },
        addHits(id, hits) {
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/job_recruitment/set?job_recruitment_id=' + id,
                data: {
                    hits: hits + 1,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {

                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
    }
}
</script>
<style lang="less"  scoped>
.sendDiv {
    font-size: 14px;
    background-color: aliceblue;
    margin-top: 50px;
}
</style>
